<template>
  <div class="index">
    <head-text titleText="专利缴费管理"></head-text>
    <div class="list-content">
      <div class="filter-area">
        <Search :searchData="searchData" :searchItem="searchItem"></Search>
        <div>
          <el-button @click="exportFee" plain type="success" size="mini"
            >导出缴费信息</el-button
          >
        </div>
      </div>
      <div class="table-title">
        <span></span>
        <h5>专利缴费列表</h5>
      </div>
      <custom-table
        :tableData="tableData"
        :tableListData="tableListData"
        :tableBtn="tableBtn"
        :currentData="currentData"
        @onPageChange="onPageChange"
        @onSizeChange="onSizeChange"
        @handleConfirm="handleConfirm"
        @handleCancel="handleCancel"
        @handleComplete="handleComplete"
        @handleViewOther="handleViewOther"
        v-if="pageReset"
      ></custom-table>
    </div>
    <dialog-com :dialogObj="dialogObj" @reflash="getList"></dialog-com>
  </div>
</template>
<script>
import customTable from "@c/table";
import Search from "@c/search";
import headText from "@c/headText";
import dialogCom from "./dialogCom";
import timeFormat from "@u/timeFormat";
import {
  getPatentFeePageApi,
  getCustomerListApi,
  confirmFeeApi,
  exportPatentFeeApi,
  rollbackFeeApi
} from "@s/api";
import { PATENTFEESTATUS, PATENTFEESTATUSLIST } from "@u/wordbook";
export default {
  name: "index",
  data() {
    return {
      nowPage: 1,
      total: 0,
      limit: 10,
      pageReset: true,
      searchItem: [],
      patentFeeStatusList: PATENTFEESTATUSLIST,
      searchData: {
        customerId: "",
        title: "",
        applicationNumber: "",
        startTime: "",
        endTime: "",
        status: ""
      },
      dialogObj: {
        id: "",
        title: "",
        show: false,
        form: {
          title: "",
          type: ""
        }
      },
      parameter: {
        id: "",
        status: ""
      },
      tableData: [],
      customerSelect: [],
      currentData: {}
    };
  },
  components: {
    headText,
    dialogCom,
    Search,
    customTable
  },
  created() {
    // form搜索
    this.searchItem = [
      {
        type: "select",
        label: "客户名称：",
        prop: "customerId",
        placeholder: "请选择",
        selectList: this.customerSelect
      },
      {
        type: "input",
        label: "专利名",
        prop: "title",
        placeholder: "请输入专利名"
      },
      {
        type: "input",
        label: "申请号",
        prop: "applicationNumber",
        placeholder: "请输入申请号"
      },
      {
        type: "daterange",
        label: "缴费截止日：",
        timeFormat: "yyyy-MM-dd",
        timeList: ["startTime", "endTime"]
      },
      {
        type: "select",
        label: "状态：",
        prop: "status",
        placeholder: "请选择",
        selectList: this.patentFeeStatusList
      }
    ];
    //  table表格
    this.tableListData = [
      { width: "50", label: "", type: "index" },
      { prop: "customerName", width: "", label: "客户名称", type: "a" },
      { prop: "title", label: "专利名", type: "a" },
      { prop: "applicationNumber", width: "", label: "申请号", type: "a" },
      { prop: "applicationDatetime", width: "", label: "申请日期", type: "a" },
      { prop: "feeType", width: "", label: "费用种类", type: "a" },
      {
        prop: "feeAmount",
        width: "",
        label: "应缴金额(元)",
        type: "wordbook",
        wordbookList: this.getFeeAmount
      },
      {
        prop: "agentAmount",
        width: "",
        label: "代理费(元)",
        type: "wordbook",
        wordbookList: this.getFeeAmount
      },
      {
        prop: "overdueAmount",
        width: "",
        label: "滞纳金金额(元)",
        type: "wordbook",
        wordbookList: this.getFeeAmount
      },
      {
        prop: "otherAmount",
        width: "",
        label: "其他金额(元)",
        type: "wordbook",
        wordbookList: this.getFeeAmount
      },
      {
        prop: "amount",
        width: "",
        label: "客户缴费总金额(元)",
        type: "wordbook",
        wordbookList: this.getFeeAmount
      },
      { prop: "expiryDate", width: "100xp", label: "缴费截止日", type: "a" },
      { prop: "annualfeeStatus", width: "", label: "监控状态", type: "a" },
      { prop: "payTime", width: "", label: "客户缴纳日期", type: "a" },
      {
        prop: "status",
        width: "",
        label: "状态",
        type: "wordbook",
        wordbookList: this.patentFeeStatus
      },
      { label: "操作", type: "btn", width: "150", fixed: "right" }
    ];
    // 按钮
    this.tableBtn = [
      {
        name: "确认缴费完成",
        btnType: "primary",
        handleFn: "handleComplete",
        type: "isShow",
        isShowStatus: "status",
        isShowValue: "0"
      },
      {
        name: "确认客户缴费",
        btnType: "primary",
        handleFn: "handleConfirm",
        type: "isShow",
        isShowStatus: "status",
        isShowValue: "1"
      },
      {
        name: "取消客户缴费",
        btnType: "danger",
        handleFn: "handleCancel",
        type: "isShow",
        isShowStatus: "status",
        isShowValue: "1"
      },
      {
        name: "确认缴费完成",
        btnType: "primary",
        handleFn: "handleComplete",
        type: "isShow",
        isShowStatus: "status",
        isShowValue: "2"
      }
    ];
  },
  mounted() {
    this.getCustomerSelect();
    this.getList();
  },
  methods: {
    // 过滤
    patentFeeStatus(val) {
      return PATENTFEESTATUS[val];
    },
    // 缴费金额
    getFeeAmount(val) {
      return timeFormat.formatFloat(val / 100, 2);
    },
    exportFee() {
      if (this.currentData.total >= 196) {
        this.$message({
          type: "warning",
          message: "导出记录过多无法使用缴费模板正常导出，建议查询条件限制小于196条记录！"
        });
      } else {
        this.$confirm(
          "按照查询条件进行导出，是否已设定好想要导出的查询条件?",
          "提示",
          {
            confirmButtonText: "是",
            cancelButtonText: "否",
            type: "warning"
          }
        ).then(() => {
          let json = JSON.parse(JSON.stringify(this.searchData));
          let now = new Date();
          var year = now.getFullYear();
          var month = now.getMonth() + 1;
          var day = now.getDate();
          if (month >= 1 && month <= 9) {
            month = "0" + month;
          }
          if (day >= 0 && day <= 9) {
            day = "0" + day;
          }
          var fileName = "专利缴费信息_" + year + month + day;
          exportPatentFeeApi(json, fileName).then(() => {});
        });
      }
    },
    // 查看
    handleViewOther(row) {
      this.dialogObj.id = row.id;
      this.dialogObj.read = true;
      this.dialogObj.show = true;
      this.dialogObj.title = "查看专利缴费信息";
      this.dialogObj.form = row;
    },
    // 确认操作
    handleConfirm(row) {
      this.$confirm("确认客户已缴费，确定后无法回退?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.parameter.id = row.id;
        this.parameter.status = "2";
        let json = JSON.parse(JSON.stringify(this.parameter));
        confirmFeeApi(json).then(() => {
          this.getList();
          this.$message({
            type: "success",
            message: "确认成功！"
          });
        });
      });
    },
    // 取消缴费
    handleCancel(row) {
      this.$confirm("确认客户未缴费，取消缴费?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let json = [row.id];
        json = JSON.parse(JSON.stringify(json));
        rollbackFeeApi(json).then(() => {
          this.getList();
          this.$message({
            type: "success",
            message: "确认成功！"
          });
        });
      });
    },
    // 完成操作
    handleComplete(row) {
      this.$confirm("确认已缴费完成，确定后无法回退?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.parameter.id = row.id;
        this.parameter.status = "3";
        let json = JSON.parse(JSON.stringify(this.parameter));
        confirmFeeApi(json).then(() => {
          this.getList();
          this.$message({
            type: "success",
            message: "确认完成！"
          });
        });
      });
    },
    onPageChange(val) {
      this.nowPage = val;
      this.getList();
    },
    onSizeChange(val) {
      this.nowPage = 1;
      this.limit = val;
      this.getList();
    },
    // 搜索事件
    getDataList(val) {
      this.searchData = val;
      this.nowPage = 1;
      this.limit = 10;
      this.pageReset = false;
      this.$nextTick(() => {
        this.getList();
        this.pageReset = true;
      });
    },
    //获取列表
    getList() {
      let json = JSON.parse(JSON.stringify(this.searchData));
      json.page = this.nowPage;
      json.limit = this.limit;
      getPatentFeePageApi(json).then(res => {
        this.tableData = res.list;
        this.currentData.total = res.total;
      });
    },
    // 获取客户列表
    getCustomerSelect() {
      getCustomerListApi().then(res => {
        let newCustomerList = [];
        res.forEach(item => {
          var newItem = {
            id: item.id,
            value: item.customerName
          };
          newCustomerList.push(newItem);
        });
        this.customerSelect = newCustomerList;
        this.searchItem.forEach(item => {
          if (item.prop == "customerId") {
            item.selectList = this.customerSelect;
          }
        });
      });
    },
    add() {
      this.dialogObj.id = "";
      this.dialogObj.read = false;
      this.dialogObj.show = true;
      this.dialogObj.title = "添加专利";
    }
  }
};
</script>
<style scoped lang="scss"></style>
